{include file='common/head' /}
<!--百度编辑器-->
<script type="text/javascript" charset="utf-8" src="__UEDITOR__/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__UEDITOR__/ueditor.all.min.js"> </script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->

<style>
    #shareImg,#shareImg,#coverImg {
        padding:3px;
        border:1px solid #999;
        border-radius: 4px;
        max-width: 400px;
    }
</style>

<!-- 表情开始 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<!-- Begin emoji-picker Stylesheets -->
<link href="__PLURING__/emoji/lib/css/nanoscroller.css" rel="stylesheet">
<link href="__PLURING__/emoji/lib/css/emoji.css" rel="stylesheet">
<!-- End emoji-picker Stylesheets -->
<!-- 表情结束 -->

<script type="text/javascript" charset="utf-8" src="__UEDITOR__/lang/zh-cn/zh-cn.js"></script>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动名称
                        </label>
                        <div class="layui-col-md6">
                            <input type="text" name="name" autocomplete="off" class="layui-input" data-emojiable="true">
                        </div>
                    </div>
                    <!-- <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动封面
                        </label>
                        <div class="layui-input-block layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal file" id="cover_1"><i class="layui-icon"></i>上传图片</button>
                            <input class="layui-upload-file" type="file" accept="undefined" name="cover" id="cover">
                            <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                            <img style="width:70px" id="cover_img" src="" alt="">
                        </div>
                    </div> -->

                    <!-- 表情开始--- -->
                    <script src="__PLURING__/jq.js"></script>
                    <script>window.jQuery || document.write('<script src="js/jquery-2.1.1.min.js"><\/script>')</script>
                    <!-- Begin emoji-picker JavaScript -->
                    <script src="__PLURING__/emoji/lib/js/nanoscroller.min.js"></script>
                    <script src="__PLURING__/emoji/lib/js/tether.min.js"></script>
                    <script src="__PLURING__/emoji/lib/js/config.js"></script>
                    <script src="__PLURING__/emoji/lib/js/util.js"></script>
                    <script src="__PLURING__/emoji/lib/js/jquery.emojiarea.js"></script>
                    <script src="__PLURING__/emoji/lib/js/emoji-picker.js"></script>
                    <!-- End emoji-picker JavaScript -->
                    <script>
                    $(function() {
                      // Initializes and creates emoji set from sprite sheet
                      window.emojiPicker = new EmojiPicker({
                        emojiable_selector: '[data-emojiable=true]',
                        assetsPath: '/static/pluring/emoji/lib/img/',
                        popupButtonClasses: 'fa fa-smile-o'
                      });
                      // Finds all elements with `emojiable_selector` and converts them to rich emoji input fields
                      // You may want to delay this step if you have dynamically created input fields that appear later in the loading process
                      // It can be called as many times as necessary; previously converted input fields will not be converted again
                      window.emojiPicker.discover();
                    });
                      </script>
                    <!-- 表情结束--- -->

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动封面
                        </label>
                        <div class="layui-input-block layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal file" id="cover_img_btn"><i class="layui-icon"></i>上传图片</button>
                            <input class="layui-upload-file" type="file" accept="undefined" id="cover_img_file">
                            <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                        </div>
                        <div>
                            <img id="coverImg" src="{$data.cover|default=''}" alt="请选择图片" />
                            <input type="hidden" name="cover" value="{$data.cover|default=''}" />
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            分享标题
                        </label>
                        <div class="layui-col-md6">
                            <input type="text" name="title" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <!-- <div class="layui-form-item">
                        <label class="layui-form-label">
                            分享图片
                        </label>
                        <div class="layui-input-block layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal file" id="test1"><i class="layui-icon"></i>上传图片</button>
                            <input class="layui-upload-file" type="file" accept="undefined" name="file" id="test1s">
                            <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                            <img style="width:70px" id="img" src="" alt="">
                        </div>
                    </div> -->
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            分享图片
                        </label>
                        <div class="layui-input-block layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal file" id="share_img_btn"><i class="layui-icon"></i>上传图片</button>
                            <input class="layui-upload-file" type="file" accept="undefined" id="share_img_file">
                            <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                        </div>
                        <div>
                            <img id="shareImg" src="{$data.share|default=''}" alt="请选择图片" />
                            <input type="hidden" name="share" value="{$data.share|default=''}" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动描述
                        </label>
                        <div class="layui-col-md6">
                            <input type="text" name="desc" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            详情图片
                        </label>
                        <div class="layui-input-block layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal file" id="detailsImg_img_btn"><i class="layui-icon"></i>上传图片</button>
                            <input class="layui-upload-file" type="file" accept="undefined" id="detailsImg_img_file">
                            <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                        </div>
                        <div>
                            <img id="detailsImg" src="{$data.detailsImg|default=''}" alt="请选择图片" />
                            <input type="hidden" name="detailsImg" value="{$data.detailsImg|default=''}" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            按钮文本
                        </label>
                        <div class="layui-col-md6">
                            <input type="text" name="btn_str" autocomplete="on" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动商品
                        </label>
                        <div class="layui-col-md6">
                            <input type="text" name="goods" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            填商品ID，多个用,隔开
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动排序
                        </label>
                        <div class="layui-col-md6">
                            <input type="text" name="sort" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            值越大，越靠前，最大值99999，不填默认0
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            有效时间
                        </label>
                        <div class="layui-col-md6">
                            <!--<input type="text" name="hours" class="layui-input" id="test5" value="{:date('Y-m-d h:i:s')}" placeholder="{:date('Y-m-d h:i:s')}">-->
                            <input type="text" name="hours" class="layui-input" value="6" placeholder=""/>
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            用户开起拼团后的有效时间
                        </div>
                    </div>
                    <!-- 结束时间 -->
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动结束时间
                        </label>
                        <div class="layui-col-md6">
                            <div class="layui-inline layui-show-xs-block">
                                <input class="layui-input"  autocomplete="off" placeholder="结束日" name="end_time" id="end_time" value="{$get.end_time|default=''}">
                            </div>
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            所有活动的结束时间
                        </div>
                    </div>
                    <!-- 参与人数 -->
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动参与人数
                        </label>
                        <div class="layui-col-md6">
                            <input type="number" name="participate_num" autocomplete="off" value="0" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            活动参与人数，显示在活动详情中
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            发货方式
                        </label>
                        <div class="layui-col-md6">
                            <input type="radio" name="deliver" value="1" title="快递配送" checked>
                            <input type="radio" name="deliver" value="2" title="现场取货">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动状态
                        </label>
                        <div class="layui-col-md6">
                            <input type="radio" name="status" value="1" title="上架" checked>
                            <input type="radio" name="status" value="0" title="下架">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            推广人数
                        </label>
                        <div class="layui-col-md6">
                            <input type="text" name="num" autocomplete="off" id="num" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            推广人数不可修改,参加该活动最多可以邀请多少人
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            分佣方式
                        </label>
                        <div class="layui-col-md6">
                            <input type="radio" name="type" value="1" title="百分比" checked>
                            <input type="radio" name="type" value="2" title="固定值">
                        </div>
                    </div>
                    <div id="role">

                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动规则
                        </label>
                        <div class="layui-input-block">
                            <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
                            </div>
                            </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="add" lay-submit="">确定保存</button></div>
                </form>
            </div>
        </div>
        <script>
            //实例化编辑器
            //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
            var ue = UE.getEditor('editor');

            layui.use(['form', 'layer','jquery','laydate'],function() {
                $ = layui.jquery;
                var form = layui.form,layer = layui.layer;
                var laydate = layui.laydate;
                //日期时间选择器
                laydate.render({
                    elem: '#end_time'
                    //,type: 'datetime'
                });

                //监听提交
                form.on('submit(add)', function(data) {
                    var num = $('[lay-submit]').index($(this));
                    var formData = new FormData($('form')[num]);
                    //formData.set('hours',Math.floor(new Date($("input[name=hours]").val()).getTime() / 1000))
                    //发异步，把数据提交给php
                    $.ajax({
                        url:"{:url('Activitys/add')}",
                        type: "post",
                        data:formData,
                        dataType:"json",
                        cache: false,                      // 不缓存
                        processData: false,                // jQuery不要去处理发送的数据
                        contentType: false,                // jQuery不要去设置Content-Type请求头
                        success:function (res) {
                            if(res.code == 200){
                                layer.alert(res.msg, {icon: 1},function () {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                            }else{
                                layer.alert(res.msg, {icon: 5});
                            }
                        },
                        error:function(e){
                            layer.alert("网络错误", {icon: 5});
                        }
                    });
                    return false;
                });

                $('#num').blur(function () {
                    var num = $('#num').val();
                    if(num==null || num==undefined || num=="")
                        return false;

                    var html = '';
                    for(var i=1;i<=num;i++){
                        html += '<div class="layui-form-item">';
                        html += '<label class="layui-form-label">第'+i+'人佣金</label>';
                        html += '<div class="layui-col-md6">';
                        html += '<input type="text" name="val[]" autocomplete="off" class="layui-input">';
                        html += '</div>';
                        html += '</div>';
                    }
                    $('#role').html(html);
                })

                //图片阅览
                $('#cover_1').click(function(){
                    $('#cover').click();
                })

                $('#cover').change(function(e){
                    var imgBox = e.target;
                    var img = getObjectURL(imgBox.files[0]);
                    $('#cover_img').attr('src',img);
                });

                //图片阅览
                $('#test1').click(function(){
                    $('#test1s').click();
                })
                $('#test1s').change(function(e){
                    var imgBox = e.target;
                    var img = getObjectURL(imgBox.files[0]);
                    $('#img').attr('src',img);
                });

                //建立一個可存取到該file的url
                function getObjectURL(file) {
                    var url;
                    if (window.createObjectURL != undefined) { // basic
                        url = window.createObjectURL(file);
                    } else if (window.URL != undefined) { // mozilla(firefox)
                        url = window.URL.createObjectURL(file);
                    } else if (window.webkitURL != undefined) { // webkit or chrome
                        url = window.webkitURL.createObjectURL(file);
                    }
                    return url;
                }

                // 绑定 活动封面
                $("#cover_img_btn").click(function(){
                    $("#cover_img_file").click();
                })

                // 操作成功后的回调 - 活动封面
                $('#cover_img_file').change(function(e){
                    var imgBox = e.target;
                    if(imgBox.files[0].size > 1024 * 1024 *2){
                        return layer.msg('失败，文件不得超过2mb',{icon: 5})
                    }
                    var img = getObjectURL(imgBox.files[0]);
                    $('#coverImg').attr('src',img);
                    var data = {image: imgBox.files[0]}
                    var formData = new FormData();
                    formData.append('image', imgBox.files[0]);
                    formData.append('prefix','activitys_cover');
                    $.ajax({
                        url:"{:url('admin/uploads/cosFiles')}",
                        type: 'post',
                        async: false,
                        data: formData,
                        dataType: 'json',
                        processData: false,// 告诉jQuery不要去处理发送的数据
                        contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                        beforeSend: function (){ //过程...
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg('加载成功', {icon: 6})
                                //$('input[name=img_url]').val(HOST_URL +'/' +  res.data.fileName + '/' + (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                //$('input[name=img]').val( (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                $('input[name=cover]').val(res.data.url)
                            } else {
                                layer.msg(res.msg || '失败，麻烦重新尝试', {icon: 5})
                            }
                        },
                        error:function(){
                            layer.msg( '失败，重新尝试，多次失败后麻烦联系开发人员', {icon: 5})
                        }
                    })
                });

                // 绑定 分享图片
                $("#share_img_btn").click(function(){
                    $("#share_img_file").click();
                })

                // 操作成功后的回调 - 活动封面
                $('#share_img_file').change(function(e){
                    var imgBox = e.target;
                    if(imgBox.files[0].size > 1024 * 1024 *2){
                        return layer.msg('失败，文件不得超过2mb',{icon: 5})
                    }
                    var img = getObjectURL(imgBox.files[0]);
                    $('#shareImg').attr('src',img);
                    var data = {image: imgBox.files[0]}
                    var formData = new FormData();
                    formData.append('image', imgBox.files[0]);
                    formData.append('prefix','activitys_share');
                    $.ajax({
                        url:"{:url('admin/uploads/cosFiles')}",
                        type: 'post',
                        async: false,
                        data: formData,
                        dataType: 'json',
                        processData: false,// 告诉jQuery不要去处理发送的数据
                        contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                        beforeSend: function (){ //过程...
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg('加载成功', {icon: 6})
                                //$('input[name=img_url]').val(HOST_URL +'/' +  res.data.fileName + '/' + (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                //$('input[name=img]').val( (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                $('input[name=share]').val(res.data.url)
                            } else {
                                layer.msg(res.msg || '失败，麻烦重新尝试', {icon: 5})
                            }
                        },
                        error:function(){
                            layer.msg( '失败，重新尝试，多次失败后麻烦联系开发人员', {icon: 5})
                        }
                    })
                });

                // 绑定 详情图片
                $("#detailsImg_img_btn").click(function(){
                    $("#detailsImg_img_file").click();
                })

                // 操作成功后的回调 - 详情图片
                $('#detailsImg_img_file').change(function(e){
                    var imgBox = e.target;
                    if(imgBox.files[0].size > 1024 * 1024 *2){
                        return layer.msg('失败，文件不得超过2mb',{icon: 5})
                    }
                    var img = getObjectURL(imgBox.files[0]);
                    $('#detailsImg').attr('src',img);
                    var data = {image: imgBox.files[0]}
                    var formData = new FormData();
                    formData.append('image', imgBox.files[0]);
                    formData.append('prefix','activitys_details');
                    $.ajax({
                        url:"{:url('admin/uploads/cosFiles')}",
                        type: 'post',
                        async: false,
                        data: formData,
                        dataType: 'json',
                        processData: false,// 告诉jQuery不要去处理发送的数据
                        contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                        beforeSend: function (){ //过程...
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg('加载成功', {icon: 6})
                                //$('input[name=img_url]').val(HOST_URL +'/' +  res.data.fileName + '/' + (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                //$('input[name=img]').val( (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                console.log(res.data.url)
                                $('input[name=detailsImg]').val(res.data.url)
                            } else {
                                layer.msg(res.msg || '失败，麻烦重新尝试', {icon: 5})
                            }
                        },
                        error:function(){
                            layer.msg( '失败，重新尝试，多次失败后麻烦联系开发人员', {icon: 5})
                        }
                    })
                });


            });
        </script>
{include file='common/scroll' /}        
{include file='common/footer' /}